//
// Created by ht on 16-12-16.
//
#include <cstdio>
#include "syntax.tab.hpp"
#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

stringstream ss;
stringstream datass, textss;
ofstream irout, asmout;

void yyrestart(FILE *f);

int main(int argc, char *argv[]) {
    if (argc < 2)
        return 1;
    FILE *f = fopen(argv[1], "r");
    if (!f) {
        perror(argv[1]);
        return 1;
    }
    ss << argv[1] << ".ir";
    irout.open(ss.str());
    ss.str("");
    ss << argv[1] << ".s";
    asmout.open(ss.str());
    ss.str("");
    yyrestart(f);
    yyparse();
    irout.close();
    asmout << ".data\n"<<"_prompt: .asciiz \"Enter an integer:\"\n_ret: .asciiz \"\\n\"\n";
    asmout<< datass.str();
    asmout << ".globl main\n";
    asmout << ".text\n";
    asmout<<"read:\n\tli $v0, 4\n\tla $a0, _prompt\n\tsyscall\n\tli $v0, 5\n\tsyscall\n\tjr $ra\n";
    asmout<<"write:\n\tli $v0, 1\n\tsyscall\n\tli $v0, 4\n\tla $a0, _ret\n\tsyscall\n\tmove $v0, $0\n\tjr $ra\n";
    asmout << textss.str();
    asmout.close();
    return 0;
}
